#include "app_sensor.h"
#include "app_storage.h"
#include "cynos_list.h"


const static char *TAG = "SENSOR";
AppSensor *sensorObj;
TaskHandle_t AppSensor::taskHandle = NULL;
TimerHandle_t AppSensor::timerHandle = NULL;

typedef void(*SensorFunc)(int arg);

typedef struct
{
   CynOSList_Hanlde list; //must be first
   SensorFunc func;
   SensorFunc inter_func;
}SensorList;
SensorList *SensorListHead=0; 

SensorList SensorMap[]=
{
    {0,0,0},
    {0,0,0},
};

static_assert((sizeof(SensorMap)/sizeof(SensorList) == static_cast<int>(SensorProcess::MAX)),"sensor map err!!!");

void AppSensor::threadtimeCallback(TimerHandle_t handle)
{
   mpu6050TimeInterrupt(SENSOR_TASK_TIME);
}

void AppSensor::process()
{
   sht20_process(SENSOR_TASK_TIME);
   mpu6050Process(SENSOR_TASK_TIME);
}

void AppSensor::init()
{
    SHT20_SetStatus(1,5000);
    MPU6050_SetStatus(1,2000);
    for(SensorProcess iii = SensorProcess::POWER; iii < (SensorProcess::MAX); iii = static_cast<SensorProcess>(static_cast<int>(iii) + 1))
    {
        CynOSListInsert((void **)(&SensorListHead),&SensorMap[static_cast<int>(iii)],1);
    }
}
void AppSensor::thread(void *a)
{
    sensorObj = new AppSensor();
    sensorObj->init();
    AppSensor::timerHandle = xTimerCreate(TAG, pdMS_TO_TICKS(SENSOR_TASK_TIME), true, NULL, AppSensor::threadtimeCallback);
    xTimerStart(AppSensor::timerHandle,pdMS_TO_TICKS(SENSOR_TASK_TIME));
    while(1)
    {
        sensorObj->process();
        vTaskDelay(pdMS_TO_TICKS(SENSOR_TASK_TIME)); 
    }
}


void AppSensor::threadCreate()
{
   xTaskCreate(AppSensor::thread, TAG, SENSOR_STACK_SIZE, NULL, SENSOR_TASK_PRIO, &(AppSensor::taskHandle));
}

